package com.ruoyi.report_lqhhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_tg.domain.TgsybgBaDetail;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.report_tg.mapper.TgsybgBaDetailMapper;
import com.ruoyi.sycs.service.IBiQualificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lqhhl.domain.bo.ReportLqhhlMxetjfbgBsInfoBo;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlMxetjfbgBsInfoVo;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlMxetjfbgBsInfo;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlMxetjfbgBsInfoMapper;
import com.ruoyi.report_lqhhl.service.IReportLqhhlMxetjfbgBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 沥青混合料马歇尔试验检测报告(体积法)Service业务层处理
 *
 * @author llb
 * @date 2024-08-19
 */
@RequiredArgsConstructor
@Service
public class ReportLqhhlMxetjfbgBsInfoServiceImpl implements IReportLqhhlMxetjfbgBsInfoService {

    private final ReportLqhhlMxetjfbgBsInfoMapper baseMapper;
    private final TgsybgBaDetailMapper detailMapper;

    private final IBiQualificationService biQualificationService;

    private static final Long[] keys = {143L,144L,145L,146L,147L,148L,149L,150L,151L,152L};

    /**
     * 查询沥青混合料马歇尔试验检测报告(体积法)
     */
    @Override
    @FIllBasicInfo
    public ReportLqhhlMxetjfbgBsInfoVo queryById(Long excelId){
        ReportLqhhlMxetjfbgBsInfoVo reportLqhhlMxetjfbgBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<TgsybgBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<TgsybgBaDetail>()
            .eq(TgsybgBaDetail::getExcelId, excelId));
        baDetailVos.forEach(v->{
            v.setQualInfo(biQualificationService.queryById(v.getQualId()));
        });
        // 格式化 技术指标
        biQualificationService.compute(baDetailVos);
        // 转换成map形式
        Map<Long, TgsybgBaDetailVo> collect = baDetailVos.stream().collect(Collectors.toMap(TgsybgBaDetailVo::getQualId, v -> {
            return v;
        }));
        reportLqhhlMxetjfbgBsInfoVo.setBaDetailVos(collect);
        return reportLqhhlMxetjfbgBsInfoVo;
    }

    /**
     * 查询沥青混合料马歇尔试验检测报告(体积法)列表
     */
    @Override
    public TableDataInfo<ReportLqhhlMxetjfbgBsInfoVo> queryPageList(ReportLqhhlMxetjfbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqhhlMxetjfbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportLqhhlMxetjfbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青混合料马歇尔试验检测报告(体积法)列表
     */
    @Override
    public List<ReportLqhhlMxetjfbgBsInfoVo> queryList(ReportLqhhlMxetjfbgBsInfoBo bo) {
        LambdaQueryWrapper<ReportLqhhlMxetjfbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqhhlMxetjfbgBsInfo> buildQueryWrapper(ReportLqhhlMxetjfbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqhhlMxetjfbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportLqhhlMxetjfbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportLqhhlMxetjfbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportLqhhlMxetjfbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), ReportLqhhlMxetjfbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), ReportLqhhlMxetjfbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportLqhhlMxetjfbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportLqhhlMxetjfbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportLqhhlMxetjfbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLqhhlMxetjfbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportLqhhlMxetjfbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportLqhhlMxetjfbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportLqhhlMxetjfbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportLqhhlMxetjfbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportLqhhlMxetjfbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportLqhhlMxetjfbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getHunhllx()), ReportLqhhlMxetjfbgBsInfo::getHunhllx, bo.getHunhllx());
        lqw.eq(StringUtils.isNotBlank(bo.getCedmdff()), ReportLqhhlMxetjfbgBsInfo::getCedmdff, bo.getCedmdff());
        lqw.eq(StringUtils.isNotBlank(bo.getBiaozsjcc()), ReportLqhhlMxetjfbgBsInfo::getBiaozsjcc, bo.getBiaozsjcc());
        lqw.eq(StringUtils.isNotBlank(bo.getJiscs()), ReportLqhhlMxetjfbgBsInfo::getJiscs, bo.getJiscs());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), ReportLqhhlMxetjfbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportLqhhlMxetjfbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), ReportLqhhlMxetjfbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportLqhhlMxetjfbgBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportLqhhlMxetjfbgBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportLqhhlMxetjfbgBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), ReportLqhhlMxetjfbgBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportLqhhlMxetjfbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportLqhhlMxetjfbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportLqhhlMxetjfbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增沥青混合料马歇尔试验检测报告(体积法)
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportLqhhlMxetjfbgBsInfoBo bo) {
        ReportLqhhlMxetjfbgBsInfo add = BeanUtil.toBean(bo, ReportLqhhlMxetjfbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 多个keys
            ArrayList<TgsybgBaDetail> details = new ArrayList<>(10);
            for (Long key : keys) {
                details.add(new TgsybgBaDetail(add.getExcelId(), key));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改沥青混合料马歇尔试验检测报告(体积法)
     */
    @Override
    @DSTransactional
    @ComputeReportResult
    public Boolean updateByBo(ReportLqhhlMxetjfbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        ReportLqhhlMxetjfbgBsInfo update = BeanUtil.toBean(bo, ReportLqhhlMxetjfbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos().values(), TgsybgBaDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(ReportLqhhlMxetjfbgBsInfoBo bo) {
        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
            for (TgsybgBaDetailVo detailVo : bo.getBaDetailVos().values()) {
                if ("不符合".equals(detailVo.getJiegpd())) {
                    bo.setExcelResult(2);
                    return;
                }
            }
            bo.setExcelResult(1);
        }
        bo.setExcelResult(0);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqhhlMxetjfbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青混合料马歇尔试验检测报告(体积法)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public ReportLqhhlMxetjfbgBsInfoVo jcjl(ReportLqhhlMxetjfbgBsInfoVo vo) {
        StringBuilder result = new StringBuilder("检测结论：依据JTG E20-2011检测，该沥青混合料");
        boolean haveShicz = false;
        for (TgsybgBaDetailVo detailVo : vo.getBaDetailVos().values()) {
            if ("实测值".equals(detailVo.getJiegpd())) {
                result.append(detailVo.getQualName()).append("为").append(detailVo.getJiancjg()).append(detailVo.getQualUnit()).append("，");
                haveShicz = true;
            }
        }
        if (haveShicz) {
            result.append("其他指标符合");
        } else {
            result.append("所检指标符合");
        }
        result.append("DB 37/T 1161-2009及设计要求，可用于路面工程/柔性基层施工。");
        vo.setJiancjl(result.toString());
        return vo;
    }
}
